<template>
  <!-- 添加或修改铁附件订单拆解明细对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="序号" prop="sort">
                <el-input-number v-model="form.sort" placeholder="请输入序号"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="单位" prop="unit">
                <el-input v-model="form.unit" placeholder="请输入单位"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="数量" prop="amount">
                <el-input-number v-model="form.amount" placeholder="请输入数量" :precision="2" @change="calculateTotalWeight"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="单重" prop="unitWeight">
                <el-input-number v-model="form.unitWeight" placeholder="请输入单重" :precision="2" @change="calculateTotalWeight"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="总重" prop="totalWeight">
                <el-input-number v-model="form.totalWeight" placeholder="请输入总重" :precision="2" disabled/>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="含税单价" prop="unitPrice">
                <el-input-number v-model="form.unitPrice" placeholder="请输含税单价" :precision="2" @change="calculateCalcTotalPrice"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="含税总价" prop="totalPrice">
                <el-input-number v-model="form.totalPrice" placeholder="请输入含税总价" :precision="2" disabled/>
              </el-form-item>
            </el-col>


            <el-col :span="12">
              <el-form-item label="物料编码" prop="materialCode">
                <el-input v-model="form.materialCode" placeholder="请输入物料编码"/>
              </el-form-item>
            </el-col>


            <el-col :span="12">
              <el-form-item label="送货通知单号" prop="noticeNumber">
                <el-input v-model="form.noticeNumber" placeholder="请输入送货通知单号"/>
              </el-form-item>
            </el-col>


            <el-col :span="12">
              <el-form-item label="领用项目编号" prop="materialCode">
                <el-input v-model="form.useProjectNumber" placeholder="请输入领用项目编号"/>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="领用项目名称" prop="useProjectName">
                <el-input v-model="form.useProjectName" placeholder="请输入领用项目名称"/>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="下单收货人" prop="orderReceiver">
                <el-input v-model="form.orderReceiver" placeholder="请输入下单收货人"/>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="下单收货人电话" prop="orderReceiverTel">
                <el-input v-model="form.orderReceiverTel" placeholder="请输入下单收货人电话"/>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="实际收货人" prop="actualReceiver">
                <el-input v-model="form.actualReceiver" placeholder="请输入实际收货人"/>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="实际收货人电话" prop="actualReceiverTel">
                <el-input v-model="form.actualReceiverTel" placeholder="请输入实际收货人电话"/>
              </el-form-item>
            </el-col>










            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" v-model="form.remark" placeholder="请输入备注"/>
              </el-form-item>
            </el-col>

          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addAction, getById, updateAction,accMul} from '@/api/manage';
import moment from 'moment';

export default {
  name: "OrderIronDetailForm",
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sort: [
          {required: true, message: "序号不能为空", trigger: "blur"},
          {validator:this.validator,trigger: 'blur'}
        ],
        unit: [
          {required: true, message: "单位不能为空", trigger: "blur"},
          {validator:this.validator,trigger: 'blur'}
        ],
        amount: [
          {required: true, message: "数量不能为空", trigger: "blur"},
          {validator:this.validator,trigger: 'blur'}
        ],
        unitWeight: [
          {required: true, message: "单重不能为空", trigger: "blur"},
          {validator:this.validator,trigger: 'blur'}
        ],
      },
      url: {
        getById: '/orderIronDetail/orderIronDetail/',
        add: '/orderIronDetail/orderIronDetail/add',
        edit: '/orderIronDetail/orderIronDetail/edit',
      },
    };
  },
  created() {

  },
  methods: {
    validator(rule, value, callback) {
      if (value === 0) {
        callback(new Error('不能为0'));
      } else {
        callback();
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        recordId: null,
        unit: null,
        amount: null,
        unitWeight: null,
        totalWeight: null,
        unitPrice: null,
        totalPrice: null,
        calcUnitWeight: null,
        calcTotalWeight: null,
        materialCode: null,
        noticeNumber: null,
        useProjectNumber: null,
        useProjectName: null,
        orderReceiver: null,
        orderReceiverTel: null,
        actualReceiver: null,
        actualReceiverTel: null,
        sort: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd(recordId,orderId) {
      this.reset();
      this.open = true;
      this.form.recordId = recordId;
      this.form.orderId = orderId;
      this.title = "添加铁附件订单拆解明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改铁附件订单拆解明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAction(this.url.edit, this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('ok');
            });
          } else {
            addAction(this.url.add, this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('ok');
            });
          }
        }
      });
    },

    calculateTotalWeight(){
      this.form.totalWeight = accMul(this.form.unitWeight, this.form.amount);
    },

    calculateCalcTotalPrice(){
      this.form.calcTotalWeight = accMul(this.form.totalPrice, this.form.amount);
    },


  }
};
</script>
